package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpDispatchInfoDAO;
import org.jsola.hr.dto.EmpDispatchInfoAddDTO;
import org.jsola.hr.dto.EmpDispatchInfoUpdateDTO;
import org.jsola.hr.entity.EmpDispatchInfoDO;
import org.jsola.hr.query.EmpDispatchInfoQuery;
import org.jsola.hr.service.IEmpDispatchInfoService;
import org.jsola.hr.vo.EmpDispatchInfoListVO;
import org.jsola.hr.vo.EmpDispatchInfoVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 派单人员非员工信息
 *
 * @author zhr
 */
@Service("hrEmpDispatchInfoServiceImpl")
public class EmpDispatchInfoServiceImpl implements IEmpDispatchInfoService {

    @Autowired
    private IEmpDispatchInfoDAO empDispatchInfoDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpDispatchInfoVO save(EmpDispatchInfoAddDTO empDispatchInfoAddDTO, TokenUser tokenUser) {
        // 转DO
        EmpDispatchInfoDO empDispatchInfoDO = empDispatchInfoAddDTO.to(EmpDispatchInfoDO.class);
        // 保存
        empDispatchInfoDO = save(empDispatchInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empDispatchInfoDO.to(EmpDispatchInfoVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpDispatchInfoUpdateDTO empDispatchInfoUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpDispatchInfoDO empDispatchInfoDO = empDispatchInfoUpdateDTO.to(EmpDispatchInfoDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empDispatchInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empDispatchInfoIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empDispatchInfoIds);
    }


    @Override
    public EmpDispatchInfoVO selectById(String empDispatchInfoId, String siteId) {
        EmpDispatchInfoDO empDispatchInfoDO = selectDOById(empDispatchInfoId, siteId);
        if (empDispatchInfoDO == null) {
            return null;
        }
        return empDispatchInfoDO.to(EmpDispatchInfoVO.class);
    }

    @Override
    public List<EmpDispatchInfoListVO> select(EmpDispatchInfoQuery empDispatchInfoQuery, String siteId) {
        List<EmpDispatchInfoDO> empDispatchInfoDOList = selectDO(empDispatchInfoQuery, siteId);
        if(CollectionUtils.isEmpty(empDispatchInfoDOList)) {
            return empDispatchInfoDOList == null ? null : new ArrayList<>();
        }
        return empDispatchInfoDOList.stream()
                .map(empDispatchInfoDO -> empDispatchInfoDO.to(EmpDispatchInfoListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpDispatchInfoQuery empDispatchInfoQuery, String siteId) {
        Example example = buildExample(empDispatchInfoQuery,siteId);
        return empDispatchInfoDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpDispatchInfoListVO> selectPage(EmpDispatchInfoQuery empDispatchInfoQuery, String siteId) {
        Example example = buildExample(empDispatchInfoQuery,siteId);
        Page<EmpDispatchInfoDO> page = empDispatchInfoDAO.selectPageByExample(example,
                empDispatchInfoQuery.getPageNo(),
                empDispatchInfoQuery.getPageSize());

        return page.to(EmpDispatchInfoListVO.class);
    }

    @Override
    public EmpDispatchInfoDO selectDOById(String empDispatchInfoId, String siteId) {
        return listById(empDispatchInfoId, siteId);
    }

    @Override
    public List<EmpDispatchInfoDO> selectDO(EmpDispatchInfoQuery empDispatchInfoQuery, String siteId) {
        Example example = buildExample(empDispatchInfoQuery,siteId);
        return empDispatchInfoDAO.selectByExample(example);
    }

    @Override
    public EmpDispatchInfoDO findByIdCard(String idCard, String siteId) {
        Example example = new Example(EmpDispatchInfoDO.class);
        example.and()
                .andEqualTo("idCard", idCard)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);

        return empDispatchInfoDAO.selectOneByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param empDispatchInfoQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpDispatchInfoQuery empDispatchInfoQuery, String siteId) {
        Example example = new Example(EmpDispatchInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empDispatchInfoQuery.getName() != null) {
            example.and().andEqualTo("name", empDispatchInfoQuery.getName());
        }
        if (empDispatchInfoQuery.getPhone() != null) {
            example.and().andEqualTo("phone", empDispatchInfoQuery.getPhone());
        }
        if (empDispatchInfoQuery.getIdCard() != null) {
            example.and().andEqualTo("idCard", empDispatchInfoQuery.getIdCard());
        }
        if (empDispatchInfoQuery.getEmpPhoto() != null) {
            example.and().andEqualTo("empPhoto", empDispatchInfoQuery.getEmpPhoto());
        }
        if (empDispatchInfoQuery.getSocCard() != null) {
            example.and().andEqualTo("socCard", empDispatchInfoQuery.getSocCard());
        }
        if (empDispatchInfoQuery.getPfCard() != null) {
            example.and().andEqualTo("pfCard", empDispatchInfoQuery.getPfCard());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empDispatchInfoQuery.getOrders());
        return example;
    }
}




